home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1264 < prev    next >
Encoding:
Text File  |  1996-08-05  |  912 b   |  54 lines

  1. Path: news.uh.edu!sukku
  2. From: sukku@menudo.uh.edu (sukumar)
  3. Newsgroups: comp.lang.c
  4. Subject: GoTo equivalent in C ??
  5. Date: 12 Jan 1996 18:06:46 GMT
  6. Organization: University of Houston
  7. Message-ID: <4d67vm$e5h@masala.cc.uh.edu>
  8. NNTP-Posting-Host: menudo.uh.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Hi,
  12.     I have always thought about this. How do you get the effect of goto
  13. in C without using "goto"?? For ex, if I have to check for an error in my
  14. function for every computation I do, and then do some cleaning up, how do 
  15. I do it. Here is an example:
  16.  
  17. f()
  18. {
  19.  
  20. char *s = (char *) malloc(10 * sizeof(char));
  21. int i;
  22.  
  23. i = scanf("%s", s);
  24.  
  25. if(i != 1) /* I wish I could do a goto to the last two lines of the function*/
  26. {
  27.     free(s); 
  28.     return;
  29. }
  30.  
  31. i = atoi(s);
  32. if(i==0)
  33. {
  34.     free(s);
  35.     return;
  36.  
  37. }
  38.  
  39.  
  40.  
  41. ...
  42. ..
  43.  
  44. free(s);
  45. return;
  46.  
  47. }
  48.  
  49.  
  50. I don't want to use goto. what alternatievs do I have.
  51. Any ideas are greatly appreciated.
  52.  
  53. -Srini.
  54.